/*
 * Created on Feb 13, 2005
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package bmoofxbridge.model;

import java.text.ParseException;
import java.util.Calendar;

import net.ofx.types.x2003.x04.StatementTransaction;
import net.ofx.types.x2003.x04.TransactionEnum;

import com.develop.jawin.COMException;

import bmoofxbridge.BMOParser;

/**
 * @author leonidr
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class CreditCardTransaction extends Transaction{

	
	public static short[] StatementDate = new short[] {0,1};
	public static short[] PaymentsTotal = new short[] {1,1};
	public static short[] TransactionAccount = new short[] {2,2};
	public static short[] TransactionMemo = new short[] {2,1};
	public static short[] TransactionName = new short[] {2,0};
	public static short[] CardType = new short[] {2,4};
	public static short[] BankAccount = new short[] {2,5};
	
	protected String memo;  
	protected String bankAccount;
	/**
	 * @param parser
	 */
	public CreditCardTransaction(BMOParser parser) {			
		try {
            ammount = parser.getValue(TransactionAmmountDouble); 
            //balance = parser.getValue(TransactionBalanceDouble);
            setRefNumber(String.valueOf(Math.round(Math.abs(getAmmount()*100))));
            date = BMOParser.fromBMODateFormat.parse(parser.getValue(TransactionDate));
            statementDate = BMOParser.fromBMODateFormat.parse(parser.getValue(StatementDate));
            
            Calendar tranDate = Calendar.getInstance();
            tranDate.setTime(date);
            
            Calendar statementStart = Calendar.getInstance();
            statementStart.setTime(statementDate);
            statementStart.add(Calendar.MONTH, -1);
            
            tranDate.set(Calendar.ERA, statementStart.get(Calendar.ERA));
            tranDate.set(Calendar.YEAR, statementStart.get(Calendar.YEAR));
            tranDate.set(Calendar.MONTH, statementStart.get(Calendar.MONTH));
            
            while (tranDate.before(statementStart)){            
            	tranDate.add(Calendar.MONTH, 1);
            }
            
            date = tranDate.getTime();  
            
            accountId = parser.getValue(TransactionAccount);
            name = parser.getValue(TransactionName);
            memo = parser.getValue(TransactionMemo);
            
            bankAccount = parser.getValue(BankAccount);
        } catch (Throwable e) {
            e.printStackTrace();
        } 
		
	}
	
	public StatementTransaction getCreditCardTransaction() throws COMException {
		StatementTransaction transaction = StatementTransaction.Factory.newInstance();
	    if (getAmmount()<0) {
            transaction.setTRNTYPE(TransactionEnum.DEBIT);
        }
        else {
            transaction.setTRNTYPE(TransactionEnum.CREDIT);
        }
	    
	    String dateStr=BMOParser.toOFXDateFormat.format(getDate());
	    transaction.setDTPOSTED(dateStr);
	    transaction.setFITID(dateStr+getRefNumber());
	    if ((null==getMemo()) || getMemo().length()==0){
	    	transaction.setMEMO(getName());
	    }
	    else{
	    	transaction.setMEMO(getMemo());
	    }	    
	    transaction.setNAME(getName());
	    transaction.setTRNAMT(String.valueOf(-getAmmount()));
	    
        
	    return transaction;
	}
	
	/**
	 * @return Returns the memo.
	 */
	public String getMemo() {
		return memo;
	}
	
	
	/* (non-Javadoc)
	 * @see bmoofxbridge.model.Transaction#getAccount()
	 */
	public Account getAccount() {
		return new CreditCardAccount("","",bankAccount,accountId);
	}
}
